package com.lechi.iot.netty.device;

import com.lechi.iot.netty.channel.ITcpChannel;
import com.lechi.iot.netty.service.NameValueImpl;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;

public class TcpDeviceChannel extends NameValueImpl implements ITcpChannel {

    private final Channel channel;

    private final String hostName;

    private String deviceSn;

    public TcpDeviceChannel(InetSocketAddress address, Channel channel) {
        this.channel = channel;
        this.hostName = address.getHostString();
        name(address.getHostString());
    }

    public TcpDeviceChannel(Channel channel) {
        this((InetSocketAddress) channel.remoteAddress(), channel);
    }

    @Override
    public String hostName() {
        return hostName;
    }

    @Override
    public void send(ByteBuf msg) {
        channel.writeAndFlush(msg);
    }

    @Override
    public String getDeviceSn() {
        return deviceSn;
    }

    @Override
    public void setDeviceSn(String deviceSn) {
        this.deviceSn = deviceSn;
    }
}
